﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using HelfenHelfen.DataAccess.Entities;
using HelfenHelfen.Logic.Factories;
using System.Web.Security;
using System.Web;
using System.Web.Configuration;
using System.Threading;

namespace HelfenHelfen.Web.Forms.Site.Services
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    public class MessageService
    {
        [OperationContract]
        [WebGet]
        public int GetMessageCount()
        {
            HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);

            var user = Membership.GetUser();
            if (user != null)
            {
                var logic = LogicFactory.CreateMessageLogic();
                return logic.GetMessageCountForUser((Guid)user.ProviderUserKey);
            }
            else
            {
                return -1;
            }
        }

        [OperationContract]
        [WebGet]
        public IEnumerable<MessageViewModel> GetMessages()
        {
            Thread.Sleep(2500);
            HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);

            var logic = LogicFactory.CreateMessageLogic();

            var userLogic = LogicFactory.CreateUserDetailLogic();

            var user = Membership.GetUser();
            if (user != null)
            {
                return logic.GetMessagesForUser(
                    (Guid)user.ProviderUserKey)
                    .Select(m => new MessageViewModel()
                    {
                        Title = m.Title,
                        Message = m.Message1,
                        MessageGUID = m.MessageGUID,
                        Sender = ConvertToJsonUser(userLogic, m.SenderUserDetailGUID)
                    });
            }
            else
                return new List<MessageViewModel>();
        }

        [OperationContract]
        [WebGet]
        public void SendMessage(MessageViewModel message)
        {
            var logic = LogicFactory.CreateMessageLogic();

            Message msg = new Message()
            {
                Title = message.Title,
                Message1 = message.Message,
                MessageGUID = Guid.NewGuid(),
                CreationDateTime = DateTime.Now
            };
            msg.SenderUserDetailGUID = (Guid)Membership.GetUser().ProviderUserKey;
            msg.RecipientUserDetailGUID = msg.SenderUserDetailGUID;

            logic.SendMessage(msg);
        }

        [OperationContract]
        [WebGet]
        public void DeleteMessage(Guid messageGUID)
        {
            var logic = LogicFactory.CreateMessageLogic();
            logic.DeleteMessage(messageGUID);
        }

        private UserDetailViewModel ConvertToJsonUser(Interfaces.IUserDetailLogic userLogic, Guid userGuid)
        {
            var user = userLogic.GetUserDetailByGuid(userGuid);
            return new UserDetailViewModel()
            {
                FirstName = user.FirstName,
                LastName = user.Lastname,
                UserDetailGUID = user.UserDetailGUID
            };
        }
    }

    [Serializable]
    [DataContract(Name = "Message")]
    public class MessageViewModel
    {
        [DataMember(IsRequired = true, Name = "Title")]
        public string Title { get; set; }
        [DataMember(IsRequired = true, Name = "Message")]
        public string Message { get; set; }
        [DataMember(IsRequired = false)]
        public Guid MessageGUID { get; set; }
        [DataMember(IsRequired = false)]
        public UserDetailViewModel Sender { get; set; }
    }

    [Serializable]
    [DataContract(Name = "UserDetail")]
    public class UserDetailViewModel
    {
        [DataMember()]
        public Guid UserDetailGUID { get; set; }
        [DataMember()]
        public string FirstName { get; set; }
        [DataMember()]
        public string LastName { get; set; }
    }
}
